package com.mapper.influx;

import com.entity.FlightRealTimeData;
import com.entity.KeyValue;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.annotation.Measurement;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @className: FlightRealTimeDataMapper
 * @description: TODO 类描述
 * @author: Li Chuanwei
 * @date: 2024/04/01 14:28
 * @Company: Copyright [日期] by [作者或个人]
 **/
@Slf4j
@Repository
public class FlightRealTimeDataMapper {
    @Autowired
    private InfluxDBMapper influxDBMapper;
    private static final String TABLE = FlightRealTimeData.class.getAnnotation(Measurement.class).name();
    private static final String DATABASE = FlightRealTimeData.class.getAnnotation(Measurement.class).database();
    public void save(FlightRealTimeData flightRealTimeData) {
        influxDBMapper.save(flightRealTimeData);
    }

    /**
     * 获取UTC时间，并减少8小时
     * @param timeStr 时间格式 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public String getUTC (String timeStr){
        //String start = "2023-1-1 "+"00:00:00";
        DateFormat dtfUTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            Date date1 = simpleDateFormat.parse(timeStr);
            Calendar ca = Calendar.getInstance();
            ca.setTime(date1);
            ca.add(Calendar.HOUR,-8);
            Date dt1=ca.getTime();
            return dtfUTC.format(dt1);
        }catch (ParseException e) {
            return "";
        }
    }

    public List<FlightRealTimeData> select(String timeId) {
        StringBuilder sql = new StringBuilder("SELECT *  FROM " + TABLE);

        // 添加时间ID条件
        sql.append(" WHERE time_id = '").append(timeId).append("'");

        List<FlightRealTimeData> records = influxDBMapper.query(new Query(sql.toString(), DATABASE), FlightRealTimeData.class);

        // 遍历 records 列表
        for (FlightRealTimeData data : records) {
            // 获取当前时间
            Instant currentTime = data.getTime();
            // 将当前时间加 8 小时
            Instant newTime = currentTime.plusSeconds(8 * 60 * 60);
            // 将新时间设置回 RealTimeData 对象
            data.setTime(newTime);
        }

//        records.forEach(i -> log.info("record={}", i));
        return records;
    }

    public List<String> getAllRecords(){
        String sql ="SHOW TAG VALUES FROM \"t_flightrealtimedata\" with key=\"time_id\"\n";
        List<KeyValue> keyValues= influxDBMapper.query(new Query(sql, DATABASE), KeyValue.class);
        List<String> values = keyValues.stream()
                .map(KeyValue::getValue)
                .collect(Collectors.toList());
        return values;
    }
}